package com.woniu.controller;


import com.woniu.entity.Buildings;
import com.woniu.service.IBuildingsService;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
//楼号信息
@RestController
public class BuildingsController {
    @Autowired
    private IBuildingsService buildingsService;



    /**
     * 查询全部
     */
    @PostMapping("/buildings/findAll")
    private ResultVO findAll() {
        List<Buildings> list = null;
        try {
            list =  buildingsService.list();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }

        return ResultVO.success(list);
    }

    /**
     * 查询一个
     */
    @PostMapping("/buildings/findById")
    private ResultVO findById(@RequestBody String bid) {
        Buildings buildings = null;
        try {
            buildings = buildingsService.getById(bid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(buildings);
    }

    /**
     * 删除一个
     */
    @PostMapping("/buildings/deleteByIds")
    private ResultVO deleteById(@RequestBody String bid) {
        int rows=0;
        try {
           rows = buildingsService.deleteById(bid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (rows > 0 ? ResultVO.success() : ResultVO.failure());
    }

    /**
     * 新增
     */
    @PostMapping("/buildings/save")
    private ResultVO save(@RequestBody Buildings buildings) {
        boolean flag = false;
        try {
            flag = buildingsService.save(buildings);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (flag ? ResultVO.success() : ResultVO.failure());

    }

    /**
     * 修改
     */
    @PostMapping("/buildings/updateById")
    private ResultVO updateById(@RequestBody Buildings buildings) {
        boolean flag = false;
        try {
            flag = buildingsService.updateById(buildings);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (flag ? ResultVO.success() : ResultVO.failure());

    }



}

